[11/11] net: WireGuard secure network tunnel
authorJason A. Donenfeld <Jason@zx2c4.com>
Sun, 8 Dec 2019 23:27:34 +0000 (00:27 +0100)
committerSalvatore Bonaccorso <carnil@debian.org>
Mon, 30 Mar 2020 21:06:57 +0000 (22:06 +0100)
commit29618bb5d411a8154c98777b617cc994fcb31943
tree1254640aaca233241c3edc076a17f7401f81b2f4
parent961b3f81cb56d4f57954d4415ddd12da6ae09de9
[11/11] net: WireGuard secure network tunnel

Origin: https://git.zx2c4.com/wireguard-linux/commit?id=37cd67dbe53d8c3afd6e98fc2bda126ee46be52c
Bug-Debian: https://bugs.debian.org/953569

WireGuard is a layer 3 secure networking tunnel made specifically for
the kernel, that aims to be much simpler and easier to audit than IPsec.
Extensive documentation and description of the protocol and
considerations, along with formal proofs of the cryptography, are
available at:

  * https://www.wireguard.com/
  * https://www.wireguard.com/papers/wireguard.pdf

This commit implements WireGuard as a simple network device driver,
accessible in the usual RTNL way used by virtual network drivers. It
makes use of the udp_tunnel APIs, GRO, GSO, NAPI, and the usual set of
networking subsystem APIs. It has a somewhat novel multicore queueing
system designed for maximum throughput and minimal latency of encryption
operations, but it is implemented modestly using workqueues and NAPI.
Configuration is done via generic Netlink, and following a review from
the Netlink maintainer a year ago, several high profile userspace tools
have already implemented the API.

This commit also comes with several different tests, both in-kernel
tests and out-of-kernel tests based on network namespaces, taking profit
of the fact that sockets used by WireGuard intentionally stay in the
namespace the WireGuard interface was originally created, exactly like
the semantics of userspace tun devices. See wireguard.com/netns/ for
pictures and examples.

The source code is fairly short, but rather than combining everything
into a single file, WireGuard is developed as cleanly separable files,
making auditing and comprehension easier. Things are laid out as
follows:

  * noise.[ch], cookie.[ch], messages.h: These implement the bulk of the
    cryptographic aspects of the protocol, and are mostly data-only in
    nature, taking in buffers of bytes and spitting out buffers of
    bytes. They also handle reference counting for their various shared
    pieces of data, like keys and key lists.

  * ratelimiter.[ch]: Used as an integral part of cookie.[ch] for
    ratelimiting certain types of cryptographic operations in accordance
    with particular WireGuard semantics.

  * allowedips.[ch], peerlookup.[ch]: The main lookup structures of
    WireGuard, the former being trie-like with particular semantics, an
    integral part of the design of the protocol, and the latter just
    being nice helper functions around the various hashtables we use.

  * device.[ch]: Implementation of functions for the netdevice and for
    rtnl, responsible for maintaining the life of a given interface and
    wiring it up to the rest of WireGuard.

  * peer.[ch]: Each interface has a list of peers, with helper functions
    available here for creation, destruction, and reference counting.

  * socket.[ch]: Implementation of functions related to udp_socket and
    the general set of kernel socket APIs, for sending and receiving
    ciphertext UDP packets, and taking care of WireGuard-specific sticky
    socket routing semantics for the automatic roaming.

  * netlink.[ch]: Userspace API entry point for configuring WireGuard
    peers and devices. The API has been implemented by several userspace
    tools and network management utility, and the WireGuard project
    distributes the basic wg(8) tool.

  * queueing.[ch]: Shared function on the rx and tx path for handling
    the various queues used in the multicore algorithms.

  * send.c: Handles encrypting outgoing packets in parallel on
    multiple cores, before sending them in order on a single core, via
    workqueues and ring buffers. Also handles sending handshake and cookie
    messages as part of the protocol, in parallel.

  * receive.c: Handles decrypting incoming packets in parallel on
    multiple cores, before passing them off in order to be ingested via
    the rest of the networking subsystem with GRO via the typical NAPI
    poll function. Also handles receiving handshake and cookie messages
    as part of the protocol, in parallel.

  * timers.[ch]: Uses the timer wheel to implement protocol particular
    event timeouts, and gives a set of very simple event-driven entry
    point functions for callers.

  * main.c, version.h: Initialization and deinitialization of the module.

  * selftest/*.h: Runtime unit tests for some of the most security
    sensitive functions.

  * tools/testing/selftests/wireguard/netns.sh: Aforementioned testing
    script using network namespaces.

This commit aims to be as self-contained as possible, implementing
WireGuard as a standalone module not needing much special handling or
coordination from the network subsystem. I expect for future
optimizations to the network stack to positively improve WireGuard, and
vice-versa, but for the time being, this exists as intentionally
standalone.

We introduce a menu option for CONFIG_WIREGUARD, as well as providing a
verbose debug log and self-tests via CONFIG_WIREGUARD_DEBUG.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: David Miller <davem@davemloft.net>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: netdev@vger.kernel.org
Signed-off-by: David S. Miller <davem@davemloft.net>
[bwh: commits e7096c131e5161fa3b8e52a650d7719d2857adfd and
 65d88d04114bca7d85faebd5fed61069cb2b632c "wireguard: selftests: import harness makefile for test suite"
 a2ec8b5706944d228181c8b91d815f41d6dd8e7b "wireguard: global: fix spelling mistakes in comments"
 43967b6ff91e53bcce5ae08c16a0588a475b53a1 "wireguard: main: remove unused include <linux/version.h>"
 d89ee7d5c73af15c1c6f12b016cdf469742b5726 "wireguard: allowedips: use kfree_rcu() instead of call_rcu()"
 9a69a4c8802adf642bc4a13d471b5a86b44ed434 "wireguard: selftests: remove ancient kernel compatibility code"
 04d2ea92a18417619182cbb79063f154892b0150 "wireguard: queueing: do not account for pfmemalloc when clearing skb header"
 736775d06bac60d7a353e405398b48b2bd8b1e54 "wireguard: socket: mark skbs as not on list when receiving via gro"
 dcfea72e79b0aa7a057c8f6024169d86a1bbc84b "net: introduce skb_list_walk_safe for skb segment walking"
 9981159fc3b677b357f84e069a11de5a5ec8a2a8 "wireguard: allowedips: fix use-after-free in root_remove_peer_lists"
 ec31c2676a10e064878927b243fada8c2fb0c03c "wireguard: noise: reject peers with low order public keys"
 f9398acba6a4ae9cb98bfe4d56414d376eff8d57 "wireguard: selftests: ensure non-addition of peers with failed precomputation"
 88f404a9b1d75388225b1c67b6dd327cb2182777 "wireguard: selftests: tie socket waiting to target pid"
 a12d7f3cbdc72c7625881c8dc2660fc2c979fdf2 "wireguard: device: use icmp_ndo_send helper"
 04ddf1208f03e1dbc39a4619c40eba640051b950 "wireguard: selftests: reduce complexity and fix make races"
 2a8a4df36462aa85b0db87b7c5ea145ba67e34a8 "wireguard: receive: reset last_under_load to zero"
 175f1ca9a9ed8689d2028da1a7c624bb4fb4ff7e "wireguard: send: account for mtu=0 devices"
 1fbc33b0a7feb6ca72bf7dc8a05d81485ee8ee2e "wireguard: socket: remove extra call to synchronize_net"
 166391159c5deb84795d2ff46e95f276177fa5fb "wireguard: selftests: remove duplicated include <sys/types.h>"
 a5588604af448664e796daf3c1d5a4523c60667b "wireguard: queueing: account for skb->protocol==0"
 2b8765c52db24c0fbcc81bac9b5e8390f2c7d3c8 "wireguard: receive: remove dead code from default packet type case"
 11a7686aa99c7fe4b3f80f6dcccd54129817984d "wireguard: noise: error out precomputed DH during handshake rather than config"
 upstream]

Gbp-Pq: Topic features/all/wireguard
Gbp-Pq: Name 0011-net-WireGuard-secure-network-tunnel.patch
55 files changed:
MAINTAINERS
drivers/net/Kconfig
drivers/net/Makefile
drivers/net/wireguard/Makefile [new file with mode: 0644]
drivers/net/wireguard/allowedips.c [new file with mode: 0644]
drivers/net/wireguard/allowedips.h [new file with mode: 0644]
drivers/net/wireguard/cookie.c [new file with mode: 0644]
drivers/net/wireguard/cookie.h [new file with mode: 0644]
drivers/net/wireguard/device.c [new file with mode: 0644]
drivers/net/wireguard/device.h [new file with mode: 0644]
drivers/net/wireguard/main.c [new file with mode: 0644]
drivers/net/wireguard/messages.h [new file with mode: 0644]
drivers/net/wireguard/netlink.c [new file with mode: 0644]
drivers/net/wireguard/netlink.h [new file with mode: 0644]
drivers/net/wireguard/noise.c [new file with mode: 0644]
drivers/net/wireguard/noise.h [new file with mode: 0644]
drivers/net/wireguard/peer.c [new file with mode: 0644]
drivers/net/wireguard/peer.h [new file with mode: 0644]
drivers/net/wireguard/peerlookup.c [new file with mode: 0644]
drivers/net/wireguard/peerlookup.h [new file with mode: 0644]
drivers/net/wireguard/queueing.c [new file with mode: 0644]
drivers/net/wireguard/queueing.h [new file with mode: 0644]
drivers/net/wireguard/ratelimiter.c [new file with mode: 0644]
drivers/net/wireguard/ratelimiter.h [new file with mode: 0644]
drivers/net/wireguard/receive.c [new file with mode: 0644]
drivers/net/wireguard/selftest/allowedips.c [new file with mode: 0644]
drivers/net/wireguard/selftest/counter.c [new file with mode: 0644]
drivers/net/wireguard/selftest/ratelimiter.c [new file with mode: 0644]
drivers/net/wireguard/send.c [new file with mode: 0644]
drivers/net/wireguard/socket.c [new file with mode: 0644]
drivers/net/wireguard/socket.h [new file with mode: 0644]
drivers/net/wireguard/timers.c [new file with mode: 0644]
drivers/net/wireguard/timers.h [new file with mode: 0644]
drivers/net/wireguard/version.h [new file with mode: 0644]
include/linux/skbuff.h
include/uapi/linux/wireguard.h [new file with mode: 0644]
tools/testing/selftests/wireguard/netns.sh [new file with mode: 0755]
tools/testing/selftests/wireguard/qemu/.gitignore [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/Makefile [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/aarch64.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/aarch64_be.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/arm.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/armeb.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/i686.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/m68k.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/mips.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/mips64.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/mips64el.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/mipsel.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/powerpc.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/arch/x86_64.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/debug.config [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/init.c [new file with mode: 0644]
tools/testing/selftests/wireguard/qemu/kernel.config [new file with mode: 0644]